(function ($) {
    $.fn.ui_stable = function (url, opts, setting) {

        var url = url;
        var opts = $.extend({}, {
            pageSize: 10,
            pageNum: 1
        }, opts);

        var setting = setting;

		function inittable($this) {
			$this.html(creategrid());
			rerush($this);
			initevent($this);
		}

		function creategrid() {
			var table = '<div class="ui-stable"><table><thead><tr>' + setting.header + '</tr></thead><tbody class="loading"></tbody></table><div class="ui-page"></div>';
			return table;
		}
		/// {
		///      'data': [{'ID':'1', 'Name':'aaa','Age':'1'],{'ID':'1', 'Name':'aaa','Age':'1'],{'ID':'1', 'Name':'aaa','Age':'1']],
		///      'total':1000,
		/// }
		function rerush($this) {
			$this.addClass("loading");
			$.ajax({type: "post", url: url, data: opts, async: false, success: function (data) {
					//data = eval('(' + data + ')');
					if (data !== "" && data.rows.length > 0) {
						$this.find("tbody").html(creatrow(data.rows));
						$this.find(".ui-page").html(createpage(opts.pageNum, opts.pageSize, data.total));
					} else {
						$this.find(".ui-page").html('<div class="ui-stable-nodata">没有记录</div>');
					}
				}
			});
			$this.removeClass("loading");
		}

		function creatrow(data) {
			var table = "";
			for (var i = 0; i < data.length; i++) {
				var row = setting.body;
				for (var j in data[i]) {
					row = row.replaceAll("#" + j, data[i][j]);
				}
				table += "<tr>" + row + "</tr>";
			}
			return table;
		}

		function createpage(pagecurrent, pagesize, total) {
			var totalpage = Math.ceil(total / pagesize);

			if (pagecurrent < 1)
				pagecurrent = 1;
			if (pagecurrent > totalpage)
				pagecurrent = totalpage;

			var page = '';
			if (pagecurrent > 1) {
				page += '<a d="' + (pagecurrent - 1) + '"><</a>';
			}
			if (pagecurrent > 6) {
				page += '<a d="1">1</a><span>...</span>';
			}
			var mon = 5 - pagecurrent > 0 ? 5 - pagecurrent : 0;
			for (var i = pagecurrent - 4; i <= +pagecurrent + 5 + mon; i++) {
				if (i >= 1 && i < pagecurrent) {
					page += '<a d=' + i + '>' + i + '</a>';
				}
				if (i == pagecurrent) {
					page += '<span class="active">' + pagecurrent + '</span>';
				}
				if (i > pagecurrent && i <= totalpage) {
					page += '<a d=' + i + '>' + i + '</a>';
				}
			}
			if (totalpage - pagecurrent > 6 && i != totalpage + 1) {
				page += '<span>...</span><a d="' + totalpage + '">' + totalpage + '</a>';
			}
			if (pagecurrent < totalpage) {
				page += '<a d="' + (+pagecurrent + 1) + '">></a>';
			}
			page += '';
			return page;
		}

		function initevent($this) {

			$this.find("thead .ui-checkbox").click(function () {
				if ($(this).hasClass("checked")) {
					$this.find("tbody .ui-checkbox").removeClass("checked");
				} else {
					$this.find("tbody .ui-checkbox").addClass("checked");
				}
			});

			$this.delegate(".ui-page a", "click", function () {
				opts.pageNum = $(this).attr("d");
				rerush($this);
			});
		}		
		
        return this.each(function () {
            inittable($(this));
        });
    };    
})(jQuery);